#!/bin/sh

#
# Copyright (c) 2013 Qualcomm Atheros, Inc.
# All Rights Reserved.
# Qualcomm Atheros Confidential and Proprietary.
#

####################################################################
#
# WLAN Initialization script
#
# This script will initialize the wlan modules required for operation
# of the AP hardware.  There are several environmental variables that
# can be set to modify the operation of the AP.  These are set to
# default values (or not used) if not defined.  Parameters are:
#
#  DFS_domainoverride   0=Unitialized (default),
#                       1=FCC Domain (FCC3, US)
#                       2=ETSI Domain (Europe)
#                       3=Japan Domain
#  DFS_usenol           1=Use channel NOL (default)
#  ATH_countrycode      Set Country Code (  Override the use of channel NOL
#                       Override default country code (-1 auto, default)
#                       Use country code 0x1ff for demo channels
#  ATH_outdoor          0=Disable (default)
#                       1=Enable
#  ATH_xchanmode        0=Disable Extended Channels
#                       1=Enable Extended Channels (default)
#  ATH_use_eeprom       0=Use Flash for Calibration Data (default)
#                       1=Use EEPROM for Calibratin Data
#  ATH_force_11a_ch     1=Force AP to use 11na channels only
#                       0=Use all channels
#  ATH_debug            Load time debug flags, 0x00000000 default
#
###################################################################

KVER=`uname -r | cut -f 1 -d '-'`
MODULE_PATH=/usr/local/lib

###################################################################
## The input parameter is either up or down.  We assume that
## if no argument is provided, UP is intended
###################################################################

echo "Args: $#"

if [ $1 != down ]; then
    #
    # Calculate the parameter values required.  Add the appropriate settings
    # to the strings
    #
    
    DFS_ARGS=""
    if [ "${DFS_domainoverride}" != "" ]; then
        DFS_ARGS="domainoverride=$DFS_domainoverride $DFS_ARGS"
    fi
    if [ "${DFS_usenol}" != "" ]; then
        DFS_ARGS="usenol=$DFS_usenol $DFS_ARGS"
    fi

    #
    # PCI Args
    #

    PCI_ARGS=""
    if [ "$ATH_countrycode" != "" ]; then
        PCI_ARGS="countrycode=$ATH_countrycode $PCI_ARGS"
    fi
    if [ "$ATH_outdoor" != "" ]; then
        PCI_ARGS="outdoor=$ATH_outdoor $PCI_ARGS"
    fi
    if [ "$ATH_xchanmode" != "" ]; then
        PCI_ARGS="xchanmode=$ATH_xchanmode $PCI_ARGS"
    fi
    if [ "${ATH_use_eeprom}" != "" ]; then
        PCI_ARGS="use_eeprom=$ATH_use_eeprom $PCI_ARGS"
    fi

    if [ "$ATH_debug" != "" ]; then
        PCI_ARGS="ath_debug=$ATH_debug $PCI_ARGS"
    fi

#
# Finally, insert the modules
#
    insmod $MODULE_PATH/adf.ko
    insmod $MODULE_PATH/asf.ko
    insmod $MODULE_PATH/ath_hal.ko
    insmod $MODULE_PATH/ath_rate_atheros.ko
    insmod $MODULE_PATH/ath_dev.ko
    insmod $MODULE_PATH/umac.ko
else
#
# First, check to see if all of the VAPs have been eliminated
# If not, kill them all.  Don't continue, since this script
# will be called by killVAP all
#

## Closes all the P2P VAPs and reboots the system

VAPLIST=`iwconfig | grep wlan | cut -b 1-5`

##
## ALL VAPs must be down before ANY can be destroyed
## Do this in two stages
##

    for i in $VAPLIST
    do
        echo "downing $i"
        ifconfig $i down || { echo "ifconfig $i down FAILED"; exit 1; }
    done

    sleep 1
    for i in $VAPLIST
    do
        echo "killing $i"
        wlanconfig $i destroy || { echo "wlanconfig $i destroy FAILED "; exit 1; }
    done

    sleep 3


    rmmod umac.ko
    rmmod ath_dev.ko
    rmmod ath_rate_atheros.ko
    rmmod ath_hal.ko
    rmmod asf.ko
    rmmod adf.ko
fi
